package com.ruoyi.project.system.controller;

import java.util.*;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.PatrolRecord;
import com.ruoyi.project.system.service.IPatrolRecordService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 巡查记录Controller
 * 
 * @author ruoyi
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/system/record")
public class PatrolRecordController extends BaseController
{
    @Autowired
    private IPatrolRecordService patrolRecordService;

    /**
     * 查询巡查记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PatrolRecord patrolRecord)
    {
        startPage();
        List<PatrolRecord> list = patrolRecordService.selectPatrolRecordList(patrolRecord);
        return getDataTable(list);
    }

    /**
     * 导出巡查记录列表
     */
    @Log(title = "巡查记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PatrolRecord patrolRecord)
    {
        List<PatrolRecord> list = patrolRecordService.selectPatrolRecordList(patrolRecord);
        ExcelUtil<PatrolRecord> util = new ExcelUtil<PatrolRecord>(PatrolRecord.class);
        return util.exportExcel(list, "巡查记录数据");
    }

    /**
     * 获取巡查记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        PatrolRecord patrolRecord = patrolRecordService.selectPatrolRecordById(id);
        List<Map<String,Object>> fileUrlList = new ArrayList<>();
        for(int i = 0; i< Arrays.asList(patrolRecord.getFileUrl().split(",")).size(); i++){
            Map<String,Object> map = new HashMap<>();
            map.put("url",Arrays.asList(patrolRecord.getFileUrl().split(",")).get(i));
            map.put("name",Arrays.asList(patrolRecord.getFileName().split(",")).get(i));
            fileUrlList.add(map);
        }
        patrolRecord.setFileUrlList(fileUrlList);
        return AjaxResult.success(patrolRecord);
    }

    /**
     * 新增巡查记录
     */
    @Log(title = "巡查记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PatrolRecord patrolRecord)
    {
        patrolRecord.setFileUrl(patrolRecord.getFileUrl()==null?"":patrolRecord.getFileUrl().substring(0,patrolRecord.getFileUrl().length()-1));
        patrolRecord.setFileName(patrolRecord.getFileName()==null?"":patrolRecord.getFileName().substring(0,patrolRecord.getFileName().length()-1));
        patrolRecord.setCheckUserId(SecurityUtils.getUserId());
        patrolRecord.setCheckUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        return toAjax(patrolRecordService.insertPatrolRecord(patrolRecord));
    }

    /**
     * 修改巡查记录
     */
    @Log(title = "巡查记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PatrolRecord patrolRecord)
    {
        patrolRecord.setFileUrl(patrolRecord.getFileUrl()==null?"":patrolRecord.getFileUrl().substring(0,patrolRecord.getFileUrl().length()-1));
        patrolRecord.setFileName(patrolRecord.getFileName()==null?"":patrolRecord.getFileName().substring(0,patrolRecord.getFileName().length()-1));
        return toAjax(patrolRecordService.updatePatrolRecord(patrolRecord));
    }

    /**
     * 删除巡查记录
     */
    @Log(title = "巡查记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(patrolRecordService.deletePatrolRecordByIds(ids));
    }
}
